package cn.tyf.service.imp;

import cn.tyf.dao.UserDao;
import cn.tyf.domain.Admin;
import cn.tyf.domain.PageBean;
import cn.tyf.domain.User;
import cn.tyf.service.UserService;
import cn.tyf.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private PageBean pageBean;

    /* 普通用户相关操作 start*/


    @Override
    public User userLogin(String name, String password) {
        return userDao.userLogin(name, MD5Util.digest16(password));
    }

    @Override
    public int addUser(User user) {
        String[] split = UUID.randomUUID().toString().split("-");
        user.setRid("wx_"+ split[0]);
        user.setFlag(2); //表示为激活
        return userDao.addUser(user);
    }

    @Override
    public Admin adminLogin(String name, String password) {
        return userDao.adminLogin(name,MD5Util.digest16(password));
    }

    /**
     * 分页查询
     * @param rid
     * @param name
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean findByPage(String rid, String name,Integer flag,Integer currentPage, Integer pageSize) {
        //1总记录数
        Integer count = userDao.findByCount(rid,name,flag);
        pageBean.setTotalCount(count);
        //总页数
        Integer totalPage = (count % pageSize==0) ? count / pageSize : count / pageSize +1;
        pageBean.setTotalPage(totalPage);
        //当前页码
        pageBean.setCurrentPage(currentPage);
        //每页记录数
        pageBean.setPageSize(pageSize);
        //数据
        int start = (currentPage - 1) * pageSize;
        List<User> byPage = userDao.findByPage(rid,name,flag,start,pageSize);
        pageBean.setList(byPage);
        return pageBean;
    }

    /**
     * 根据id删除
     * @param uid
     * @return
     */
    @Override
    public boolean deleteById(String uid) {
        userDao.deleteById(Integer.parseInt(uid));
        return true;
    }

    /**
     * 根据id查询
     * @param uid
     * @return
     */
    @Override
    public User findById(String uid) {
        return userDao.findById(Integer.parseInt(uid));
    }

    /**
     * 根据密码查询，用于更新密码前的判断用户输入的原密码是否正确
     * @param password
     * @return
     */
    @Override
    public User findByPassword(Integer id,String password) {
        return userDao.findByPassword(id,MD5Util.digest16(password));
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    /**
     * 根据id更新用户信息
     * @param user
     */
    @Override
    public void updateUser(User user) {
        User user1 = userDao.findById(user.getId());
        if (user.getPassword().equals(user1.getPassword())){
            userDao.updateById(user.getName(),user.getPassword(),user.getFlag(),user.getId());
        }else {
            userDao.updateById(user.getName(),MD5Util.digest16(user.getPassword()),user.getFlag(),user.getId());
        }

    }

    /**
     * 更新用户权限状态
     * @param flag
     * @param id
     */
    @Override
    public void updateFlag(Integer flag,Integer id) {
        userDao.updateFlag(flag,id);
    }


    /* 普通用户相关操作 end*/

    /* 管理员相关操作 start*/

    @Override
    public Admin findByAdminId(Integer aid) {
        return userDao.findByAdminId(aid);
    }

    /**
     * 管理员更新
     * @param admin
     */
    @Override
    public void updateAdmin(Admin admin) {
        userDao.updateByAdminId(MD5Util.digest16(admin.getPassword()),admin.getId());
    }

    @Override
    public User findByRid(String card) {
        return userDao.findByRid(card);
    }

    @Override
    public PageBean findByAdmin(String name, Integer currentPage, Integer pageSize) {
        //1总记录数
        Integer count = userDao.findByAdminCount(name);
        pageBean.setTotalCount(count);
        //总页数
        Integer totalPage = (count % pageSize==0) ? count / pageSize : count / pageSize +1;
        pageBean.setTotalPage(totalPage);
        //当前页码
        pageBean.setCurrentPage(currentPage);
        //每页记录数
        pageBean.setPageSize(pageSize);
        //数据
        int start = (currentPage - 1) * pageSize;
        List<Admin> byPage = userDao.findByAdminPage(name,start,pageSize);
        pageBean.setList(byPage);
        return pageBean;
    }

    @Override
    public void deleteByAdminId(Integer aid) {
        userDao.deleteByAdminId(aid);
    }

    @Override
    public Boolean insertAdmin(Admin admin) {
        Admin admin1 = userDao.findbyAdminName(admin.getName());
        if (admin1 == null){
            userDao.insertAdmin(admin.getName(),admin.getGender(),MD5Util.digest16(admin.getPassword()),admin.getPower());
            return true;
        }
        return false;
    }
    /**
     * 根据密码查询，用于更新密码前的判断用户输入的原密码是否正确
     * @param password
     * @return
     */
    @Override
    public Admin findByAdminPassword(Integer id,String password) {
        return userDao.findByAdminPassword(id,MD5Util.digest16(password));
    }

    @Override
    public Admin adminLogin2(String name, String password) {
        return userDao.adminLogin(name,password);
    }

    @Override
    public User userLogin2(String name, String password) {
        return userDao.userLogin(name,password);
    }

    /* 管理员相关操作 end*/

}
